#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e3 + 10;
int h[N], e[N], ne[N], w[N];
int index;
int dis[N], flag[N];
int m, n;
void add(int a, int b, int c)
{
	w[index] = c;
	e[index] = b;
	ne[index] = h[a];
	h[a] = index++;
}
int spfa()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[1] = 0;
	queue<int>q;
	q.push(1);
	flag[1] = 1;
	while (q.size()) {
		int temp = q.front();
		q.pop();
		flag[temp] = 0;
		for (int i = h[temp]; i != -1; i = ne[i]) {
			int j = e[i];
			if (dis[j] > dis[temp] + w[i]) {
				dis[j] = dis[temp] + w[i];
				if (!flag[j]) {
					q.push(j);
					flag[j] = 1;
				}
			}
		}
	}
	if (dis[n] == 0x3f3f3f3f) {
		return -1;
	}
	else {
		return dis[n];
	}
}
int main()
{
	memset(h, -1, sizeof(h));
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
	}
	cout << spfa() << endl;
	return 0;
}